<template>
    <div id="s_cargo">
        <div class="s_cargo_header">
            <h3>欢迎使用购物机</h3>
            <div class="title">
                <span>已获取积分：{{coin}}积分<small>（1积分抵扣1元）</small></span>
            </div>
        </div>
        <div class="content">
            <div v-for="item in cargo" v-on:click="go(item.passage,item.stock)" class="clearfix">
                <div class="fl">
                    <h4 >{{item.name}}</h4>
                    <small>{{item.price}}积分/个</small>
                </div>
                <img class="fr" src="../../static/img/right.png" alt="" >
                <small class="fr" v-if="item.stock>0" style="color: #888888">剩余{{item.stock}}个</small>
                <small class="fr" v-else style="color: #888888">已售完</small>
            </div>
        </div>
    </div>
</template>
<style lang="less">
    @import '../less/s_cargo.less';
</style>
<script>
    import jw from '../../node_modules/jquery-weui/dist/js/jquery-weui.min.js'

    function GetRequest(strName) {
        var strHref = window.location.href
        var intPos = strHref.indexOf("?");
        var strRight = strHref.substr(intPos + 1);
        var arrTmp = strRight.split("&");
        for (var i = 0; i < arrTmp.length; i++) {
            var arrTemp = arrTmp[i].split("=")
            if (arrTemp[0].toUpperCase() == strName.toUpperCase()) {
                return arrTemp[1]
            }
        }
        return '9';
    }

    export default {
        components: {},
        data() {
            return {
                coin: 0,
                cargo: [
                    {
                        name: '玩具',
                        price: '1.00',
                        stock: '12',
                        presentType: '',
                        img: '../../static/img/cigarette.png'
                    },
                    {
                        name: '纸巾',
                        price: '1.00',
                        stock: '12',
                        presentType: '',
                        img: '../../static/img/cigarette.png'
                    },
                    {
                        name: '纸巾',
                        price: '1.00',
                        stock: '12',
                        presentType: '',
                        img: '../../static/img/cigarette.png'
                    },
                    {
                        name: '纸巾',
                        price: '1.00',
                        stock: '0',
                        presentType: '',
                        img: '../../static/img/cigarette.png'
                    },
                ]
            }
        },
        methods: {
            imgUrl(type) {
                switch (type) {
                    case '纸巾':
                        return 'http://ow452wcle.bkt.clouddn.com/tissue.png'
                        break;
                    case '饮品':
                        return 'http://ow452wcle.bkt.clouddn.com/drink.png'
                        break;
                    case '零食':
                        return 'http://ow452wcle.bkt.clouddn.com/snacks.png'
                        break;
                    case '香烟':
                        return 'http://ow452wcle.bkt.clouddn.com/cigarette.png'
                        break;
                    case '玩具':
                        return 'http://ow452wcle.bkt.clouddn.com/toys.png'
                        break;
                    case '一次性雨衣':
                        return 'http://ow452wcle.bkt.clouddn.com/raincoat.png'
                        break;
                    case '其他':
                        return 'http://ow452wcle.bkt.clouddn.com/other.png'
                        break;
                }
            },
            go(passage, stock) {
                if (stock == 0) {
                    $.toptip('该商品售完，请选择别的商品购买', 1500, 'warning')
                } else {
                    window.location.href = 'http://www.nowdone.com.cn/sIndex/' + passage
                }
            }
        },
        mounted() {
            var code = GetRequest("code")
            if (code == '1') {
                $.toptip('领取游戏币成功', 1500, 'success')
            } else if (code == '2') {
                $.toptip('游戏币已领取', 1500, 'error')
            } else if (code == '0') {
                $.toptip('领取失败,请重新扫描二维码', 1500, 'warning')
            } else if (code == '9') {
                console.log('index')
            }
            const eid = localStorage.getItem("eid")
            this.$http.get('/api/v1/wechat/index/' + eid)
                .then(function (res) {
                    if (res.data.code == 1) {
                        this.coin = res.data.data.coin;
                        this.cargo = res.data.data.cargo;
                        console.log(this.cargo)
                    }
                }, function (err) {
                    console.log(err)
                })
        }
    }
</script>